package com.cloud.base.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.base.entity.BaseUser;
import com.cloud.base.service.BaseUserService;

@RestController
public class BaseUserController {

	@Autowired
	BaseUserService baseUserService;

	@GetMapping(value = "/getBaseUser/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public BaseUser getBaseUser(@PathVariable("id") Integer id, HttpServletRequest req) {
		BaseUser entity = new BaseUser();
		entity.setId(id);
		entity.setName("admin" + new Date().toGMTString());
		entity.setPassword("hsd" + id);
		entity.setAddress(req.getRequestURL().toString());
		baseUserService.save(entity);
		return entity;
	}

	@GetMapping(value = "/list")
	public List<BaseUser> findBaseUser() {
		return baseUserService.findBaseUsers();
	}

	@GetMapping(value = "/myQuery", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<BaseUser> myQuery() {
		return baseUserService.myQuery();
	}

	@GetMapping(value = "/findOne/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public BaseUser findOne(@PathVariable("id") String id) {
		return baseUserService.findOne(id);
	}

}
